var app = getApp()
var fetchApi = require("../../utils/httpPost.js");
Page({
    data: {
        imgurl: app.getImgUrl(),
        userInfo: {},
        hasUserInfo: false,
        orderItemArr: [],
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        adBanner: [],
        hasBanner: false,
        total_integral: 0,
        shop_type: 0,
        isLoading: false,
        raiseUserId: 0,
    },
    onLoad: function (options) {
        console.log("login   options", options)
        var that = this
        if (options.ag_id) {
            this.data.ag_id = parseInt(options.ag_id)
        }
        // 邀请合养
        if (options.raiseUserId) {
            this.data.raiseUserId = parseInt(options.raiseUserId)
        }
        var isLogin = app.getStorageSync("isLogin");
        if (isLogin) {
            wx.redirectTo({
                url: 'pages/home/home',
            })
        }
    },
    onGetUserInfo: function (e) {
        console.log('onGetUserInfo e', e)
        // this.loginOp(e.detail)
        wx.getUserProfile({
            desc: '用与获取用户信息',
            success: (res) => {
                console.log('onGetUserInfo ', res)
                this.loginOp(res)
            }
        })
    },
    loginOp: function (res_user) {
        var that = this
        that.setData({
            isLoading: true
        })
        app.setStorageSync("isLogin", 0);
        var longitude = app.getStorageSync('lng');
        var latitude = app.getStorageSync('lat');
        // 登录
        wx.login({
            success: res => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                console.log("code", res.code)
                fetchApi.httpPost('/wxUser/wxLogin', {
                    code: res.code,
                    ag_id: that.data.ag_id,
                    longitude: longitude,
                    latitude: latitude,
                    raiseUserId:that.data.raiseUserId,
                }, function (res) {
                    console.log("登录返回数据", res);
                    if (res.data.code == 0) {
                        app.globalData.user_id = res.data.data.user_id;
                        app.setStorageSync("isLogin", 1);
                        app.setStorageSync("user_id", res.data.data.user_id);
                        app.setStorageSync("access_token", res.data.data.access_token);
                        app.setStorageSync("kf_phone", res.data.data.kfPhone);
                        app.globalData.userInfo = res_user.userInfo
                        app.setStorageSync("userInfo", res_user.userInfo);
                        console.log('res_user', res_user)
                        if (res_user.errMsg == 'getUserInfo:fail auth deny') {
                            app.setStorageSync("isLogin", 0);
                            var pages = getCurrentPages();
                            var prevPage = pages[pages.length - 2]; //上一个页面
                            console.log('res_user', res_user)
                            //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
                            prevPage.setData({
                                from_page: 'login',
                            })
                            wx.navigateBack({
                                delta: 1
                            });
                            // Toast.fail('登录失败11')
                        } else {
                            fetchApi.httpPost('/wxUser/getUserInfo', {
                                iv: res_user.iv,
                                encryptedData: res_user.encryptedData,
                                rawData: res_user.rawData
                            }, function (res_info) {
                                console.log('res_info', res_info)
                                var pages = getCurrentPages();
                                console.log('pages', pages)
                                var prevPage = pages[pages.length - 2]; //上一个页面
                                console.log('pages prevPage 1', prevPage)
                                //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
                                prevPage.setData({
                                    from_page: 'login',
                                })
                                console.log('pages prevPage 2 ', prevPage)
                                wx.navigateBack({
                                    delta: 1
                                });

                            }, function () {})
                            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                            // 所以此处加入 callback 以防止这种情况
                            if (that.userInfoReadyCallback) {
                                console.log('发起回调')
                                that.userInfoReadyCallback(res_user)
                            }
                        }
                    } else {
                        wx.showToast({
                            title: '登录失败',
                        })
                    }

                    that.setData({
                        isLoading: false,
                        loading: true
                    })
                }, function () {})
            }
        })
    }
})